package com.wzt.lianfirecontrol.adapter.message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.message.MessageListModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class MessageListAdapter extends BaseRecyclerAdapter<MessageListModel> {

    private Context context;

    private boolean needSetTopMargin;
    /**
     * 是否为多选删除状态
     */
    private boolean isChooseDelete = false;

    public boolean isChooseDelete() {
        return isChooseDelete;
    }

    public void setChooseDelete(boolean chooseDelete) {
        isChooseDelete = chooseDelete;
        notifyDataSetChanged();
    }

    private MessageListCallBack messageListCallBack;

    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public MessageListAdapter(Context context, MessageListCallBack messageListCallBack) {
        this.context = context;
        this.messageListCallBack = messageListCallBack;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_message_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, MessageListModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout ll_item_content;
        private ImageView iv_item_choose;
        private ImageView iv_item_img;
        private TextView tv_title;
        private TextView tv_vice_title;
        private ImageView iv_time;
        private TextView tv_time;

        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.iv_item_choose = itemView.findViewById(R.id.iv_item_choose);
        myViewHolder.iv_item_img = itemView.findViewById(R.id.iv_item_img);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_vice_title = itemView.findViewById(R.id.tv_vice_title);
        myViewHolder.iv_time = itemView.findViewById(R.id.iv_time);
        myViewHolder.tv_time = itemView.findViewById(R.id.tv_time);
    }

    private void setItemData(MyViewHolder viewHolder, MessageListModel data, int position) {
        if (isChooseDelete && "0".equals(data.getReadFlag())){// 未读不可删除
           viewHolder.iv_item_choose.setVisibility(View.VISIBLE);
           if (data.isSelected()){
               viewHolder.iv_item_choose.setImageResource(R.mipmap.icon_single_select);
           }else {
               viewHolder.iv_item_choose.setImageResource(R.mipmap.icon_single_unselect);
           }
        }else {
            viewHolder.iv_item_choose.setVisibility(View.GONE);
            data.setSelected(false);
        }
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position && needSetTopMargin){
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15));
        }else {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
        }
        if ("0".equals(data.getReadFlag())){
            viewHolder.iv_item_img.setVisibility(View.GONE);
        }else {
            viewHolder.iv_item_img.setVisibility(View.VISIBLE);
        }
        if (StringUtils.isEmpty(data.getTitle())){
            viewHolder.tv_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getTitle());
        }

        if (StringUtils.isEmpty(data.getContent())){
            viewHolder.tv_vice_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_vice_title.setVisibility(View.VISIBLE);
            viewHolder.tv_vice_title.setText(data.getContent());
        }

        if (StringUtils.isEmpty(data.getCreateTime())){
            viewHolder.tv_time.setVisibility(View.GONE);
        }else {
            viewHolder.tv_time.setVisibility(View.VISIBLE);
            viewHolder.tv_time.setText(data.getCreateTime().replace("T", " "));
        }

        viewHolder.ll_item_content.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!isChooseDelete) {
                    messageListCallBack.deleteMessage();
                    isChooseDelete = true;
                    notifyDataSetChanged();
                }
                return true;
            }
        });
    }

    public interface MessageListCallBack{
        void deleteMessage();
    }

}
